#include "comm.hpp"

int main()
{
    //1.用fkid获取k
    key_t fkid = ftok(P_PATH,PROJ_ID);
    if(fkid == -1)
    {
        perror("ftok");
        exit(1);
    }
    //2.创建共享内存
    int shmid = shmget(fkid,SIZE,IPC_CREAT | IPC_EXCL | 0666);
    if(shmid == -1)
    {
        perror("shmid");
        exit(2);
    }
    //3.挂接共享内存
    char* shmaddr = (char*)shmat(shmid,nullptr,0);

    //4.通信
    //直接读取内存
    // while(true)
    // {
        
    //     printf("shm say : %s\n",shmaddr);
    //     if(strcmp(shmaddr,"quit") == 0)
    //         break;
        
    //     sleep(1);
    // }
    sleep(5);
    printf("shm say : %s\n",shmaddr);
    
    //5.取消挂接
    int n = shmdt(shmaddr);

    //6.删除共享内存
    int t = shmctl(shmid,IPC_RMID,nullptr);
    return 0;
}